package com.rain.cy.business.service;

import com.rain.cy.business.model.dto.UserFeedbackDTO;
import com.rain.cy.business.model.entity.UserEntity;
import com.rain.cy.business.model.entity.UserFeedbackEntity;
import com.rain.cy.business.mapper.UserFeedbackMapper;
import com.rain.cy.framework.entity.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.el.stream.Stream;
import org.assertj.core.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 患者反馈 服务实现类
 * </p>
 *
 * @author yuchen
 * @since 2021-10-02
 */
@Service
public class UserFeedbackService extends BaseServiceImpl<UserFeedbackMapper, UserFeedbackEntity> {

    @Autowired
    private UserService userService;

    public Map<String, List<UserFeedbackDTO>> getUserFeedbackMapList(Integer userId) {
        List<UserFeedbackEntity> list = lambdaQuery().eq(UserFeedbackEntity::getDoctorId, userId).list();
        return list.stream()
                .filter(userFeedbackEntity -> StringUtils.isNotEmpty(userFeedbackEntity.getTag()))
                .map(userFeedbackEntity -> UserFeedbackDTO.of(userService.getById(userFeedbackEntity.getUserId()), userFeedbackEntity))
                //按照tag进行分组
                .collect(Collectors.groupingBy(userFeedbackDTO -> userFeedbackDTO.getUserFeedbackEntity().getTag(),Collectors.toList()));

    }


    public void saveFeedback(UserFeedbackEntity userFeedbackEntity) {
        Integer userId = userFeedbackEntity.getUserId();
        UserEntity userEntity = userService.getById(userId);
        userFeedbackEntity.setUserName(userEntity.getName());
        userFeedbackEntity.setCheckType(userEntity.getCheckType());
        userFeedbackEntity.setDoctorId(userEntity.getDoctorId());
        save(userFeedbackEntity);
    }
}
